package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Permission;
import com.itheima.health.service.PermissionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @date ：Created in 2019/7/5
 * @description ：权限控制层
 * @version: 1.0
 */
@Slf4j
@RestController
@RequestMapping("/permission")
@PreAuthorize("hasRole('ROLE_ADMIN')")
public class PermissionController {

    @Reference
    private PermissionService permissionService;

    /**
     * 分页查询
     * 调用service查询分页结果
     * true 返回带内容的result
     * false 初始化结果集  返回
     *
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {

        log.debug("queryPageBean:{}", queryPageBean);

        try {

            return permissionService.pageQuery(queryPageBean);

        } catch (Exception e) {
            e.printStackTrace();
        }

        return new PageResult(0L, null);

    }

    /**
     * 传入权限信息,调用service保存
     * true 返回成功信息
     * false 返回失败信息
     *
     * @param permission
     * @return
     */
    @RequestMapping("/add")
    public Result add(@RequestBody Permission permission) {

        log.debug("permission:{}", permission);

        try {

            if (permission != null) {

                permissionService.add(permission);

                return new Result(true, MessageConst.ADD_PERMISSION_SUCCESS);

            } else {

                log.debug("Permission is null");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, MessageConst.ADD_PERMISSION_FAIL);

    }

    /**
     * 基于ID，删除权限
     * 1.调用Service删除权限
     * 2.true，返回成功消息
     * 3.false，返回异常消息
     *
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    public Result delete(Integer id) {
        try {

            permissionService.deleteById(id);

            return new Result(true, MessageConst.DELETE_PERMISSION_SUCCESS);

        } catch (Exception e) {

            e.printStackTrace();

            return new Result(false, e.getMessage());
        }
    }


    /**
     *  基于ID，获取权限数据
     * 1. 调用Service,基于ID获取权限对象
     * 2. true，返回权限对象数据
     * 3. false，返回操作失败消息
     * @param id 检查项ID
     * @return
     */
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try{

            Permission permission = permissionService.findById(id);

            return new Result(true,MessageConst.ACTION_SUCCESS,permission);

        }catch(Exception e){
            e.printStackTrace();

            return new Result(false,MessageConst.ACTION_FAIL);
        }
    }

    /**
     * 基于ID，更新权限数据
     * 1.调用Service，更新数据
     * 2.true，返回编辑成功消息
     * 3.false，返回编辑失败消息
     * @param checkItem 检查项表单数据
     * @return
     */
    @RequestMapping("/edit")
    public Result edit(@RequestBody  Permission permission){
        try{

            log.debug("permission:{}",permission);

            permissionService.edit(permission);

            return new Result(true,MessageConst.EDIT_PERMISSION_SUCCESS);
        }catch(Exception e){
            e.printStackTrace();

            return new Result(false,MessageConst.EDIT_PERMISSION_FAIL);
        }
    }

}
